package com.fleafinder.xmlparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.fleafinder.CommonConstants;
import com.fleafinder.entity.Category;


public class CategoryXmlParser {
	
	public static final String TAG_CATEGORIES = "categories";
	
	public static final String TAG_CATEGORY = "category";
	public static final String TAG_ID = "id";
	public static final String TAG_TITLE = "title";
	
	public static final int I_TAG_CATEGORY = 1;
	public static final int I_TAG_ID = 2;
	public static final int I_TAG_TITLE = 3;
	
	private List<Category> categoryList = null;
	
	private int currentTag = -1;
	
	public CategoryXmlParser(){
		this.categoryList = new ArrayList<Category>();
	}
	
	public List<Category> getCategoryList(){
		
		Category visAlle = new Category();
		visAlle.setId(CommonConstants.VIEW_ALL_ID);
		visAlle.setName(CommonConstants.VIEW_ALL);
		categoryList.add(visAlle);
		
		return categoryList;
	}
	
	/**
	 * @param in
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public void parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, CommonConstants.ENCODING_UTF_8);
            
            readCategoryEntries(parser);
            
        } catch(Exception e){
        	e.printStackTrace();
        } finally {
            in.close();
        }
    }
	
	public void readCategoryEntries(XmlPullParser parser){
		try {
			int tag = parser.next();
			Category c = null;
			while (tag != XmlPullParser.END_DOCUMENT) {
				if (tag == XmlPullParser.START_TAG) {
					if (TAG_CATEGORY.equals(parser.getName())) {
						currentTag = I_TAG_CATEGORY;
						c = new Category();

						readCategoryEntry(parser, c);
						this.categoryList.add(c);
						
//						Log.i("1", "Finish reading category id = " + c.getId());
					}
				} else if(tag == XmlPullParser.END_TAG && TAG_CATEGORIES.equals(parser.getName())){
					break;
				}
				tag = parser.next();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	
	/**
	 * @param parser
	 * @param m
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public Category readCategoryEntry(XmlPullParser parser, Category c) throws XmlPullParserException, IOException{
			
		while(true){
			int next = parser.getEventType();
			
			if(next == XmlPullParser.END_TAG){
				if(currentTag == I_TAG_CATEGORY){
					break;
				} else {
					currentTag = I_TAG_CATEGORY;
				}
			} else if(next == XmlPullParser.START_TAG){
				if(TAG_ID.equals(parser.getName())){
					currentTag = I_TAG_ID;
				} else if(TAG_TITLE.equals(parser.getName())){
					currentTag = I_TAG_TITLE;
				}
			} else if(next == XmlPullParser.TEXT){
				if(currentTag == I_TAG_ID){
					c.setId(Integer.parseInt(parser.getText()));
				} else if(currentTag == I_TAG_TITLE){
					c.setName(parser.getText());
				}
			}
			
			parser.next();
		}
		return c;
	}
}
